using System;
using System.IO;
using TMPro;
using UnityEngine;

public class DRHDebugManager : MonoBehaviour
{
	public bool DebugModeEnabled;

	[SerializeField]
	private GameObject DebugCanvas;

	[SerializeField]
	private TextMeshProUGUI DebugConsoleOutput;

	[SerializeField]
	private GameObject SpeedUpIcon;

	private string output;

	[TextArea(10, 10)]
	public string controls;

	[SerializeField]
	private TextMeshProUGUI DebugFPSCounter;

	[SerializeField]
	private AudioSource debugSource;

	public static DRHDebugManager instance;

	private bool hasToggledMenu;

	[SerializeField]
	private AudioClip sfx_fiddlesticks;

	[SerializeField]
	private AudioClip sfx_uprankmode;

	[SerializeField]
	private AudioClip sfx_notuprankmode;

	private string lastStackTrace;

	private void Awake()
	{
		instance = this;
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
	}

	private void Start()
	{
		PlayerManager.Instance._PMove._anim.SetBool("UPRANKMODE", SettingsManager.IntToBool(PlayerPrefs.GetInt("DEBUG_UprankMode", 0)));
	}

	private void Update()
	{
		if (DebugModeEnabled)
		{
			DebugCanvas.SetActive(value: true);
			DebugFPSCounter.text = Mathf.Round(1f / Time.unscaledDeltaTime).ToString();
		}
		else
		{
			DebugCanvas.SetActive(value: false);
		}
		if (!Input.anyKey)
		{
			hasToggledMenu = false;
		}
		if (Input.GetKey(KeyCode.B) && Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.Return) && !hasToggledMenu)
		{
			hasToggledMenu = true;
			DebugModeEnabled = !DebugModeEnabled;
			if (DebugModeEnabled)
			{
				output = "";
				DebugConsoleOutput.text = "";
				Debug.Log("<color=green>DEBUG FREAKING MODE ENABLED!! WOAHH!!</color>");
			}
		}
		if (DebugModeEnabled && Input.GetKey(KeyCode.B) && Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.P) && !hasToggledMenu)
		{
			hasToggledMenu = true;
			DebugConsoleOutput.enabled = !DebugConsoleOutput.enabled;
			if (DebugModeEnabled)
			{
				Debug.Log("<color=green>Toggled output visibility</color>");
			}
		}
		if (DebugModeEnabled && Input.GetKey(KeyCode.B) && Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.G) && !hasToggledMenu)
		{
			hasToggledMenu = true;
			if (Time.timeScale != 1f)
			{
				Time.timeScale = 1f;
				SpeedUpIcon.SetActive(value: false);
			}
			else
			{
				Time.timeScale = 100f;
				SpeedUpIcon.SetActive(value: true);
			}
		}
		if (DebugModeEnabled && Input.GetKey(KeyCode.B) && Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.H) && !hasToggledMenu)
		{
			hasToggledMenu = true;
			if (Time.timeScale != 1f)
			{
				Time.timeScale = 1f;
				SpeedUpIcon.SetActive(value: false);
			}
			else
			{
				Time.timeScale = 0.1f;
				SpeedUpIcon.SetActive(value: true);
			}
		}
		if (DebugModeEnabled && Input.GetKey(KeyCode.B) && Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.U) && !hasToggledMenu)
		{
			hasToggledMenu = true;
			PlayerManager.Instance._PMove._anim.SetBool("UPRANKMODE", !PlayerManager.Instance._PMove._anim.GetBool("UPRANKMODE"));
			PlayerPrefs.SetInt("DEBUG_UprankMode", SettingsManager.BoolToInt(PlayerManager.Instance._PMove._anim.GetBool("UPRANKMODE")));
			if (PlayerManager.Instance._PMove._anim.GetBool("UPRANKMODE"))
			{
				CutsceneUtils.PlaySound(sfx_uprankmode);
			}
			else
			{
				CutsceneUtils.PlaySound(sfx_notuprankmode);
			}
		}
		if (Input.GetKeyDown(KeyCode.F8) && !string.IsNullOrEmpty(lastStackTrace))
		{
			SaveStackTraceToFile();
		}
	}

	private void OnEnable()
	{
		Application.logMessageReceived += HandleLog;
	}

	private void OnDisable()
	{
		Application.logMessageReceived -= HandleLog;
	}

	private void HandleLog(string logString, string stackTrace, LogType type)
	{
		switch (type)
		{
		case LogType.Error:
		case LogType.Assert:
		case LogType.Exception:
			PlayErrorSound();
			hasToggledMenu = true;
			DebugModeEnabled = true;
			output = output + "\n<color=red>" + logString + "</color>\n<color=purple>哦——非得死定，我好恼。</color>\n<color=#00ffff>请按F8，把报错信息与栈追踪输出到txt文件。</color>";
			lastStackTrace = "主要报错信息【反馈请截这部分】\n" + logString + "\n栈追踪【反馈QQ：797416533】9\n" + stackTrace;
			break;
		case LogType.Warning:
			output = output + "\n<color=yellow>" + logString + "</color>\n";
			break;
		case LogType.Log:
			output = output + "\n<color=white>" + logString + "</color>";
			break;
		}
		if (DebugModeEnabled)
		{
			DebugConsoleOutput.text = output + controls;
		}
		else
		{
			DebugConsoleOutput.text = controls;
		}
	}

	private void PlayErrorSound()
	{
		if (!debugSource.isPlaying)
		{
			debugSource.PlayOneShot(sfx_fiddlesticks);
		}
	}

	private void SaveStackTraceToFile()
	{
		string text = DateTime.Now.ToString("hhmm");
		string text2 = Path.Combine(Application.persistentDataPath, text + "stacktrace.txt");
		File.WriteAllText(text2, lastStackTrace);
		Application.OpenURL("file://" + text2);
		Debug.Log("<color=green>Stack trace saved to " + text2 + "</color>");
	}
}
